<?php
class topic_process{

	private $db ;
	private $db_obj_topic ;

	function __construct( $db , $request = null ){
	
		$this->db = $db ;
		$this->db_obj_topic = new db_obj_topic( $db );
		$this->db_obj_topic->set( 'post_time' , date( "Y-m-d H:i:s",time()+8*60*60 ) );

		if ( !empty($request) ){
			
			foreach( $request as $k => $v ){
	
				$this->db_obj_topic->set( $k , $v ); 
			}
		}	
	}

	function get( $k ){
	
		return $this->db_obj_topic->get( $k );	
	}
	//设置主题状态
   function set_status( $status ){ 
   
     	$this->db_obj_topic->set( 'status' , $status ) ;

	}
	function set_username( $username ){

		return $this->db_obj_topic->set( 'username' , $username ) ;
	}

	function load( $k , $v ){

		return $this->db_obj_topic->load( $k , $v );
	}

	function save(){
	
		return $this->db_obj_topic->save();
	}

	function plus_view_count(){

		$view_count = $this->db_obj_topic->get( 'view_count' );
		$this->db_obj_topic->set( 'view_count' , $view_count+1 ) ;

		return true ;
	}
	function plus_reply_count(){
	
		$reply_count = $this->db_obj_topic->get( 'reply_count' );
		$this->db_obj_topic->set( 'reply_count' , $reply_count+1 );

		return true ;
	}

	//$level 0 ~ 9
	function set_hot( $level ){
	
		
	}

	static function get_all( $db ){
		
		$sql = 'SELECT * FROM topic' ;
		$res = $db->query( $sql ); 
		if (PEAR::isError($res)) {

			throw new Exception( '获得topic列表出错'.$res->getMessage() );
		}

		if ( $res->NumRows() >= 1 ){
			
			$rows = $res->fetchAll(MDB2_FETCHMODE_ASSOC) ;
			return $rows ;
		
		}else{
			
			return false ;	
		}		
	}
    static function get_all_by_username( $db,$username ){
		
		$sql = "SELECT * FROM topic WHERE username ='$username'" ;
		$res = $db->query( $sql ); 
		if (PEAR::isError($res)) {

			throw new Exception( '获得topic列表出错'.$res->getMessage() );
		}

		if ( $res->NumRows() >= 1 ){
			
			$rows = $res->fetchAll(MDB2_FETCHMODE_ASSOC) ;
			return $rows ;
		
		}else{
			
			return false ;	
		}		
	}
	function get_all_reply( ){
		
		$topic_no = $this->db_obj_topic->get( 'topic_no' );
	
		$sql = "SELECT * FROM reply WHERE `topic_no` = '$topic_no'";
		$res = $this->db->query( $sql ); 
		if (PEAR::isError($res)) {

			throw new Exception( '获得reply列表出错'.$res->getMessage().$sql );
		}

		if ( $res->NumRows() >= 1 ){
			
			$rows = $res->fetchAll(MDB2_FETCHMODE_ASSOC) ;
			return $rows ;
		
		}else{
			
			return false ;	
		}
	}
	function delete(){
	   $this->db_obj_topic->delete('topic_no',$this->db_obj_topic->get('topic_no'));
	}

}
?>
